﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
 


namespace DbSharp.Framework.Driver.FieldPersistence
{


    /// <summary>
    /// Convert field value to string, or restore string to original numeric type
    /// </summary>
    /// <remarks>This software can only support those scalar datatype can convert to and from System.String</remarks>
    public interface IFieldPersistence
    {

        /// <summary>
        /// all scalar data types supported by this software
        /// </summary>
        List<int> ListSupportedScalarDataType { get; }


        /// <summary>
        /// all supported recordset data types
        /// </summary>
        List<int> ListSupportedRecordsetDataType { get; }


        /// <summary>
        /// This software can only support those data type can convert to and from System.String
        /// </summary>
        /// <param name="value">value</param>
        /// <param name="valueType">value type</param>
        /// <returns>value charactered content</returns>
        string ConvertToString(object value, int valueType);


        /// <summary>
        /// try to restore charactered number into System.Decimal type
        /// </summary>
        /// <param name="valueStr">value content</param>
        /// <param name="valueType">type of value</param>
        /// <returns>decimal value</returns>
        decimal RestoreStringToDecimal(string valueStr, int valueType);


        /// <summary>
        /// try to restore charactered number into higer precision/scale decimal type than System.Decimal
        /// </summary>
        /// <param name="valueStr">value content</param>
        /// <param name="valueType">value type</param>
        /// <returns>decimal value</returns>
        Devart.Data.Oracle.OracleNumber RestoreStringToLargeDecimal(string valueStr, int valueType);


        /// <summary>
        /// Get specific field type in DataReader
        /// </summary>
        /// <param name="dataReader">dataReader</param>
        /// <param name="columnIndex">field ordinal index</param>
        /// <returns>
        /// field type ordinal in specific enum, 
        ///  e.g. (int)OleDbType.Decimal
        ///</returns>
        int GetSpecificDbType(DbDataReader dataReader, int columnIndex);


        /// <summary>
        /// Get specific field type in DataReader
        /// </summary>
        /// <param name="dbParam">DbParameter object</param>
        /// <returns>
        /// field type ordinal in specific enum, 
        ///  e.g. (int)OleDbType.Decimal
        /// </returns>
        int GetSpecificDbType(DbParameter dbParam);



        /// <summary>
        /// default number dbtype value when parse text into numeric, 
        /// especially when parse IntervalOfNumAxis (1,100)
        /// </summary>
        int NumberTypeWhenParseLiteral { get; }


        /// <summary>
        /// Indicate the valueType is string or not
        /// e.g. varchar, nvarchar are string type
        /// </summary>
        /// <param name="valueType"></param>
        /// <returns></returns>
        bool IsStringType(int valueType);
    }


    public static class FieldPersistenceHelper
    {
        public static string SafeConvertToString(object value)
        {
            if ((value == null) || (value is DBNull))
                return string.Empty;
            else
                return value.ToString();
        }

    }

 
  }
